#include <bits/stdc++.h>
using namespace std;
#define int long long
int N=4e18;
void solve()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    s="0"+s;
    vector<int>v(n+1);
    vector<vector<int>> w(5,vector<int>(n+1));
    for(int i=1;i<=n;i++)cin>>v[i];
    int j=0;
    //w1翻1，2没1，3翻0，4没0.
    w[1][0]=w[2][0]=w[3][0]=N;
    for(int i=1;i<=n;i++)
    {
        if(s[i]!=s[i-1])
        {
            j++;
            if(s[i]=='0')
            {
                w[1][j]=min({min(w[2][j-1],w[4][j-1])+v[i],w[1][j-1],w[3][j-1]});
                w[2][j]=N;
                w[3][j]=N;
                w[4][j]=min(w[4][j-1],w[3][j-1]+v[i]);
            }
            else 
            {
                w[1][j]=N;
                w[2][j]=min({w[2][j-1],w[4][j-1],min(w[1][j-1],w[3][j-1])+v[i]});
                w[3][j]=min(w[4][j-1]+v[i],w[3][j-1]);
                w[4][j]=N;
            }
        }
        //cout<<w[1][j]<<" "<<w[2][j]<<" "<<w[3][j]<<" "<<w[4][j]<<endl;
    }
    cout<<min({w[1][j],w[2][j],w[3][j],w[4][j]})<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}